package com.check.controller;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.check.pojo.PageObject;
import com.check.pojo.Role;
import com.check.pojo.SysLog;
import com.check.service.SysLogService;
import com.check.util.StringUtil;

@Controller
public class SysLogController extends BaseController{
	
	
	@Autowired
	@Qualifier("sysLogService")
	private SysLogService sysLogService;
	
	@RequestMapping("sysLogQuery.do")
	public ModelAndView SysLogQuery(HttpServletRequest request,HttpServletResponse response, 
								@ModelAttribute("pageObject") PageObject pageObject,
								@ModelAttribute("sysLog") SysLog sysLog) {
		if(pageObject.getCurrent() == 0){
			pageObject.setCurrent(1);
		}
		if(sysLog.getStartTime()==null){
			sysLog.setStartTime("");
		}
		if(sysLog.getEndTime()==null){
			sysLog.setEndTime("");
		}
		if(sysLog.getlIp()==null){
			sysLog.setlIp("");
		}
		if(sysLog.getOprName()==null){
			sysLog.setOprName("");
		}		
		if(sysLog.getOrgName()==null){
			sysLog.setOrgName("");
		}
		if(sysLog.getLdes()==null){
			sysLog.setLdes("");
		}
		List<SysLog> sysLogList = sysLogService.getAllSysLog(pageObject, sysLog);
//		saveSysLog(RE_SYSLOG, LOG_QUERY, request);
		pageObject.setTotal(sysLogService.getCountSysLog(sysLog));
		pageObject.setUrl("sysLogQuery.do?startTime="+sysLog.getStartTime()+"&endTime="+sysLog.getEndTime()+"&lIp="+sysLog.getlIp()+"&orgName="+sysLog.getOrgName()+"&oprName="+sysLog.getOprName()+"&ldes="+sysLog.getLdes()+"&");
		return new ModelAndView("page/syslog/sysLogList")
        .addObject("path", request.getContextPath())
        .addObject("log",sysLog)
        .addObject("sysLogList", sysLogList)
        .addObject("page", pageObject.toPage());
		
	}
	
	@RequestMapping("sysLogDelete1.do")
	public void sysLogDelete1(HttpServletRequest request,HttpServletResponse response) {
			PrintWriter out=null;
			String flag="0";
			try {
				response.setContentType("text/html;charset=gbk");
				out=response.getWriter();
				String[] ids =request.getParameterValues("uuids");
				for (String id : ids) {
					sysLogService.deleteSysLog(id);
				}
				flag="1";
			} catch (Exception e) {
				e.printStackTrace();
			}finally{
				out.print(flag);
				if(out!=null){
					out.flush();
					out.close();
				}
				
			}
			
			
	}
	
	@RequestMapping("sysLogDelete.do")
	public ModelAndView sysLogDelete(HttpServletRequest request,HttpServletResponse response) {
				String[] ids =request.getParameterValues("uuids");
				for (String id : ids) {
					sysLogService.deleteSysLog(id);
				}
//				saveSysLog(RE_SYSLOG, LOG_DELETE, request);
			return new ModelAndView("redirect:/sysLogQuery.do");
			
			
	}
}
